﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using RooHui.Infrastructure.Entities.EFContext;

namespace RooHui.Infrastructure.Entities.Repository
{
    [Table("P_Enterprise")]
    public class P_Enterprise : IEntity<P_Enterprise>
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int ID { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(128)]
        public string EnterpriseName { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(128)]
        public string EnterpriseCode { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(32)]
        public string EnterpriseCredit { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(512)]
        public string EnterpriceInfo { get; set; }
        [Column(TypeName = "nvarchar")]
        [StringLength(1024)]
        public string CapitalOperation { get; set; }

        public override P_Enterprise Find(string key)
        {
            int Id = Int32.Parse(key);
            return Find(c => c.ID == Id);
        }

        public override P_Enterprise Find(System.Linq.Expressions.Expression<Func<P_Enterprise, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Enterprise.FirstOrDefault(where);
                if (item != null)
                {
                    this.CapitalOperation = item.CapitalOperation;
                    this.EnterpriceInfo = item.EnterpriceInfo;
                    this.EnterpriseCode = item.EnterpriseCode;
                    this.EnterpriseCredit = item.EnterpriseCredit;
                    this.EnterpriseName = item.EnterpriseName;
                    this.ID = item.ID;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<P_Enterprise, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.Enterprise.Any(where);
            }
        }
    }
}